<?php
import( 'de.ceus-media.framework.krypton.core.Logic' );
import( 'classes.model.Run' );
import( 'classes.model.Project' );
//import( 'de.ceus-media.framework.krypton.core.database.StatementBuilder' );
/**
 *	Logic of Licencee, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
/**
 *	Logic of Licencee, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
class Logic_Run extends Framework_Krypton_Core_Logic
{
	function addRun( $data, $prefix = "" )
	{
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( 'management/run', 'addRun', $data, $prefix );
		$model	= new Model_Project( $data['project_id'] );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_project_id' );
		$model	= new Model_Run();
		$data['created']	= time();
		return $model->insert( $data );
	}
	
	function editRun( $runId, $data, $prefix = "" )
	{
		if( !$runId )
		 	throw new Framework_Krypton_Exception_Logic( 'run_id_missing' );
		$model	= new Model_Run( $runId );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_run_id' );
		$data	= $this->removePrefixFromFields( $data, $prefix );
		if( $data['status'] == 2 )
			$data['started']	= time();			
		else if( $data['status'] == 5 )
			$data['finished']	= time();			
		$this->validateForm( 'management/run', 'editRun', $data, $prefix );
		$result	= $model->update( $data );
		if( $result )
			$model->update( array( 'modified' => time() ) );
		return $result;
	}
	
	static function startRun( $runId )
	{
		$model	= new Model_Run( $runId );
		$data	= array(
			'status'	=> 2,
			'started'	=> time()
		);
		$model->update( $data );
	}
	
	static function closeRun( $runId )
	{
		$model	= new Model_Run( $runId );
		$data	= array(
			'status'	=> 5,
			'finished'	=> time()
		);
		$model->update( $data );
	}
	
	static function getRunData( $runId )
	{
		$model	= new Model_Run( $runId );
		$data	= $model->get();
		if( !$data )
			throw new Framework_Krypton_Exception_Logic( 'invalid_run_id' );
		return $data;
	}
	
	static function getRunDescription( $runId )
	{
		$data	= $this->getRunData( $runId );
		return $data['description'];
	}
	
	static function getRunTitle( $runId )
	{
		$data	= $this->getRunData( $runId );
		return $data['title'];
	}
	
	static function getRunList( $serverProjectId )
	{
		$model	= new Model_Run();
		$conditions	= array( 'server_project_id' => $serverProjectId );
		$order		= array( 'created' => "ASC" );
		return $model->find( array(), $conditions, $order );
	}
}
?>
